package com.sansecy.monitor.see;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.IdRes;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.sansecy.monitor.R;
import com.sansecy.monitor.widget.DragImageView;

/**
 * @Description 容器视图，整个视图添加到decorView
 * @Date 2019-08-03 10:56
 * @Created by caoyu
 */
public class SeeView extends RelativeLayout {

    private static SeeView sInstance;

    private final LogView mLogView;
    private static boolean isShow = false;

    public static SeeView getInstance() {
        if (sInstance == null) {
            sInstance = new SeeView(See.sApplication);
        }
        return sInstance;
    }

    @SuppressLint("ClickableViewAccessibility")
    private SeeView(Context context) {
        super(context);
        setId(seeViewId);

        mLogView = new LogView(context);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        this.addView(mLogView, layoutParams);

        ImageView imageView = new DragImageView(context);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        imageView.setLayoutParams(params);
        imageView.setImageResource(R.drawable.ditingicon);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShow) {
                    mLogView.setVisibility(GONE);
                    isShow = false;
                } else {
                    mLogView.setVisibility(VISIBLE);
                    isShow = true;
                }
            }
        });
        mLogView.setVisibility(GONE);

        this.addView(imageView);
    }

    @SuppressLint("ResourceType")
    @IdRes
    public static int seeViewId = 226980;

    public void attachToWindow(Activity activity) {
        ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
        ViewParent parent = getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(this);
        }
        decorView.addView(this);
    }

    public void detachFromWindow(Activity activity) {
        ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
        decorView.removeView(this);
    }

    public LogView getLogView() {
        return mLogView;
    }
}
